﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Prod;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using System.Configuration;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManagePickingTaskList : BaseForm.frmBaseMdiChildForm
    {
        private ProdPickingMaterialEntity mSelectedPickingMaterial;
        private List<ProdPickingMaterialEntity> listSelectedPickingMaterial=new List<ProdPickingMaterialEntity>();
        private int nDeliveryDateWarningTime=1;

        public frmManagePickingTaskList()
        {
            InitializeComponent();
            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["PickingPlanDeliveryDateWarningTime"].ToString()) == false)
            {
                nDeliveryDateWarningTime = Convert.ToInt32(ConfigurationManager.AppSettings["PickingPlanDeliveryDateWarningTime"].ToString());
            }
        }

        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PIK", true);

            List<MstCommonItemEntity> list1 = MstCommonItemBs.GetMstCommonItemListByType("PIK");
            repositoryItemLookUpEdit1.DataSource = list1;
            repositoryItemLookUpEdit1.ValueMember = "ItemCode";
            repositoryItemLookUpEdit1.DisplayMember = "ItemName";
            List<MstCommonItemEntity> list2 = MstCommonItemBs.GetMstCommonItemListByType("PIKT");
            repositoryItemLookUpEdit2.DataSource = list2;
            repositoryItemLookUpEdit2.ValueMember = "ItemCode";
            repositoryItemLookUpEdit2.DisplayMember = "ItemName";
        }
        private void SetField()
        {
            cmbStatus.SetSelectedValue("PIK01");
        }
        private string GetCondition()
        {
            string sCondition = "";
            if (string.IsNullOrEmpty(txtPickingOrderCode.Text) == false)
            {
                sCondition += "and PPM.PICKING_CODE='" + txtPickingOrderCode.Text + "' ";
            }
            if (cmbStatus.SelectedValue!=null&&string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                sCondition += "and PPM.STATUS='" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            return sCondition;
        }
        private void BindGrid()
        {
            string sCondition=GetCondition();
            string sOrderBy=" PPM.PLAN_DELIVERY_DATE";
            DataTable dt=ProdPickingMaterialBs.GetByCondition(sCondition, sOrderBy, txtRowCount.Text);

            DataColumn dtCol = new DataColumn();
            dtCol.ColumnName = "IS_SELECTED";
            dtCol.DataType = System.Type.GetType("System.Boolean");
            dtCol.DefaultValue = false;
            dt.Columns.Add(dtCol);

            gvMain.DataSource = dt;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            SetField();
            BindGrid();
            Timer time = new Timer();
            time.Interval = 60 * 1000;
            time.Tick += new EventHandler(time_Tick);
            time.Start();
        }

        private void time_Tick(object sender, EventArgs e)
        {
            BindGrid();
        }
        private void txtPickingOrderCode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
                return;
            if (string.IsNullOrEmpty(txtPickingOrderCode.Text))
            {
                MessageBox.Show("放料单不可为空！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtPickingOrderCode.Focus();
                return;
            }
            ProdPickingMaterialEntity mPickingMaterial = ProdPickingMaterialBs.GetProdPickingMaterialByCode(txtPickingOrderCode.Text);
            if (mPickingMaterial == null)
            {
                MessageBox.Show("放料单号错误！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPickingOrderCode.Focus();
                return;
            }
            mSelectedPickingMaterial = mPickingMaterial;
            BindGrid();
        }

        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (string.IsNullOrEmpty(gridView1.GetDataRow(e.RowHandle)["PLAN_DELIVERY_DATE"].ToString())==true)
                return;
            DateTime dtDeliveryTime=(DateTime)gridView1.GetDataRow(e.RowHandle)["PLAN_DELIVERY_DATE"];
            TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan ts2 = new TimeSpan(dtDeliveryTime.Ticks);
            TimeSpan ts = ts2.Subtract(ts1);
            if (ts.Days <= nDeliveryDateWarningTime)
            {
                e.Appearance.ForeColor = Color.Red;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void btnPicking_Click(object sender, EventArgs e)
        {
            listSelectedPickingMaterial = new List<ProdPickingMaterialEntity>();
            DataTable dt = (DataTable)gvMain.DataSource;
            foreach(DataRow dr in dt.Rows)
            {
                if ((bool)dr["IS_SELECTED"] == true)
                {
                    ProdPickingMaterialEntity m = new ProdPickingMaterialEntity(dr);
                    listSelectedPickingMaterial.Add(m);
                }
            }
            frmMaintainPickingTask frm = new frmMaintainPickingTask();
            frm.listSelectedPickingMaterial = listSelectedPickingMaterial;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }

        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            DataRow dr = ((DataRowView)gridView1.GetFocusedRow()).Row;
            if ((bool)dr["IS_SELECTED"])
            {
                dr["IS_SELECTED"] = false;
            }
            else
            {
                dr["IS_SELECTED"] = true;
            }
        }
    }
}
